/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sfs2x.extension.fourinarow.main;

import com.smartfoxserver.v2.annotations.Instantiation;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.SFSRoomRemoveMode;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.entities.variables.RoomVariable;
import com.smartfoxserver.v2.entities.variables.SFSRoomVariable;
import com.smartfoxserver.v2.exceptions.SFSCreateRoomException;
import com.smartfoxserver.v2.exceptions.SFSJoinRoomException;
import com.smartfoxserver.v2.exceptions.SFSRuntimeException;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;
import com.smartfoxserver.v2.game.CreateSFSGameSettings;
import java.util.ArrayList;
import java.util.List;
import sfs2x.extension.fourinarow.utils.Commands;

/**
 *
 * @author dodinhchien
 */
@Instantiation(Instantiation.InstantiationMode.SINGLE_INSTANCE)
public class CreateGameRequest extends BaseClientRequestHandler {

    private Integer roomGameIdCreate = 0;

    @Override
    public void handleClientRequest(User user, ISFSObject sfsObject) {
        if (!sfsObject.containsKey("xu")) {
            throw new SFSRuntimeException("Invalid request, one mandatory param is missing. Required 'xu'");
        }
        FourinarowExtension extension = (FourinarowExtension) getParentExtension();
        int xu = sfsObject.getInt("xu");
        synchronized (user) {
            if (extension.getUsersList().get(user.getId()).getMark() >= xu) {
                List<Room> lrooms = getParentExtension().getParentZone().getRoomList();
                Room roomToJoin = null;
                boolean joined = false;
                for (Room room : lrooms) {

                    if (room.isGame() && room.getMaxUsers() > room.getSize().getUserCount() && roomToJoin == null) {
                        if (room.getVariable("xu").getIntValue() == xu) {
                            roomToJoin = room;
                        }
                    }
                    if (room.containsUser(user) && !joined) {
                        joined = true;
                    }
                }

                if (!joined) {
                    if (roomToJoin == null) {
                        String groupid = sfsObject.getUtfString("group");
                        CreateSFSGameSettings settings = new CreateSFSGameSettings();
                        settings.setName("Game " + roomGameIdCreate);
                        settings.setGroupId(groupid);
                        settings.setMinPlayersToStartGame(2);
                        settings.setMaxUsers(2);
                        settings.setDynamic(true);
                        settings.setAutoRemoveMode(SFSRoomRemoveMode.WHEN_EMPTY);
                        settings.setLeaveLastJoinedRoom(true);
                        RoomVariable rv = new SFSRoomVariable("xu", xu);
                        List<RoomVariable> list = new ArrayList<>();
                        list.add(rv);
                        settings.setRoomVariables(list);
                    //CreateSFSGameSettings.RoomExtensionSettings roomsets = new CreateSFSGameSettings.RoomExtensionSettings("gameFourinarow", "sfs2x.extension.gameFourinarow.main.GameExtension");
                        //settings.setExtension(roomsets);
                        try {
                            getApi().createRoom(getParentExtension().getParentZone(), settings, user, true, user.getLastJoinedRoom());
                            roomGameIdCreate++;
                        } catch (SFSCreateRoomException ex) {
                            trace(ExtensionLogLevel.WARN, "ploblem with create game", ex.toString());
                        }
                    } else {
                        try {
                            getApi().joinRoom(user, roomToJoin, null, false, user.getLastJoinedRoom());
                        } catch (SFSJoinRoomException ex) {
                            trace(ExtensionLogLevel.WARN, "ploblem with join game", ex.toString());
                        }
                    }
                }
            }else{
                ISFSObject error = new SFSObject();
                error.putUtfString("errorMassage", "may khong du tien choi. Nap tien di");
                send(Commands.CMD_ERROR, error, user);
            }
        }

    }

}
